package com.plumejob.executor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.plumejob.executor.util.HttpsClient;

/**
 * 心跳任务
 *
 * @author : pdl
 * @date : 2021/2/5 18:12
 */
public class HeartBeatScheduler {

    public static void run() {
        PlumeJobContext.isMaster = doHeartBeat();
    }

    private static int doHeartBeat() {
        String body = null;
        try {
            body = HttpsClient.get(PlumeJobContext.host + "/plumejob/heartBeat?appName=" + PlumeJobContext.appName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (body == null) {
            return 0;
        }

        SyncResponse<HeartBeatDetail> response = JSON.parseObject(body, new TypeReference<SyncResponse<HeartBeatDetail>>() {
        });
        if (response.isOk()) {
            return response.getData().getMaster();
        }
        return 0;
    }

}
